<template>
  <div class="question">
    <div class="question-content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="详情" prop="content">
          <el-input type="textarea" v-model="ruleForm.content"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        title: '我是问题标题',
        content: '我是问题内容'
      },
      rules: {
        title: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请填写活动形式', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this._create();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    async _create() {
      let res = await this.$http.create(this.ruleForm);
      if (res.code === 0) {
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.$router.replace('/home/store');
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style lang="less" scoped>
.question {
  width: 100%;
  padding: 50px 30px 30px;
  background-color: #ffffff;
  .question-content {
    width: 80%;
  }
}
</style>
